package net.zhengxinyun.performance.controller.workSupervision;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.appCtrl.BaseCtrl;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.entity.WorkScoreEntity;
import net.zhengxinyun.performance.entity.WorkSupervisionEntity;
import net.zhengxinyun.performance.filter.WorkScoreFilter;
import net.zhengxinyun.performance.filter.WorkSupervisionFilter;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.workSupervision.WorkScoreService;
import net.zhengxinyun.performance.service.workSupervision.WorkSupervisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hyp1202 on 2018/12/13.
 */
@RestController
@RequestMapping("/workScore")
public class WorkScoreController extends BaseCtrl {

    private TokenService tokenService;
    private WorkSupervisionService workSupervisionService;
    private WorkScoreService workScoreService;
    private LoginService loginService;

    @Autowired
    public WorkScoreController(TokenService tokenService, WorkSupervisionService workSupervisionService, WorkScoreService workScoreService, LoginService loginService) {
        this.tokenService = tokenService;
        this.workSupervisionService = workSupervisionService;
        this.workScoreService = workScoreService;
        this.loginService = loginService;
    }

    @PostMapping("/insert")
    public Result<String> insertScore(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){

            if(ObjectUtils.isBlank(param.workScoreEntity.getIndexId()))
            {
                return ResultUtils.getResults(Result.Status.ERROR, "ID空值，请重新操作", null);
            }

            String str = workScoreService.insert(param.workScoreEntity);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }
    @PostMapping("/delete")
    public Result<String> deleteScore(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = workScoreService.delete(param.workScoreEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/update")
    public Result<String> updateScore(@RequestBody WorkScoreParam param){

        if (tokenService.compareToken(param.tokenEntity.getValue())){
            if(ObjectUtils.isBlank(param.workScoreEntity.getId()))
            {
                return ResultUtils.getResults(Result.Status.ERROR, "ID空值，请重新操作", null);
            }

            String str = workScoreService.update(param.workScoreEntity);
            if (!"修改成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"admin".equals(userEntity.getUserPermissions())){
                param.workScoreFilter.setLeadUnit(userEntity.getUserDepartment());
            }
            List<WorkScoreFilter> list =  workScoreService.query(param.workScoreFilter);
            if (list.size() == 0){
                param.workScoreFilter.setPageNumber(param.workScoreFilter.getPageNumber()-1);
                list = workScoreService.query(param.workScoreFilter);
            }
            Double pageTotal = Util.intGetTotalPage(workScoreService.queryByTwoTableCount(param.workScoreFilter),param.workScoreFilter.getPageSize());
            HashMap<String,Object> map = new HashMap<>();

            for (WorkScoreFilter item : list) {
                item.setCooperateUnits( Util.strToList(item.getCooperateUnit()) );

            }

            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/importScore")
    public Result<String> importScore(@RequestParam(value = "file") MultipartFile file) {
        String str = workScoreService.importScore(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }

  /*  @PostMapping("/audit")
    public Result<HashMap<String,Object>> queryForAudit(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){

            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"admin".equals(userEntity.getUserPermissions())){
                param.workScoreFilter.setLeadUnit(userEntity.getUserDepartment());
            }
            List<WorkScoreFilter> list = workScoreService.queryForAudit(param.workScoreFilter);
            if (list.size() == 0){
                param.workScoreFilter.setPageNumber(param.workScoreFilter.getPageNumber()-1);
                list = workScoreService.queryForAudit(param.workScoreFilter);
            }
            Double pageTotal = Util.intGetTotalPage(workScoreService.queryForAuditCount(param.workScoreFilter),param.workScoreFilter.getPageSize());
            HashMap<String,Object> map = new HashMap<>();
            map.put("list",list);
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }*/

    @RequestMapping("/noPass")
    public Result<String> noPass(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = workScoreService.noPass(param.workScoreEntity);
            if (!"回退成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    /**
     * 管理员审核
     * @param param
     * @return
     */
    @PostMapping("/auditProdure")
    public Result<String> pass(@RequestBody WorkScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String user = loginService.getUserByToken(param.tokenEntity).getUserPermissions();
            String str;
            String  audit= param.workScoreEntity.getAudit();
            if(StringUtils.isBlank(audit)|| "0".equals(audit)||ObjectUtils.isBlank(param.workScoreEntity.getId()))
            {
                if(ObjectUtils.isBlank(param.workScoreEntity.getId()))
                {
                    return ResultUtils.getResults(Result.Status.ERROR, "专项工作未填写分数，无法审核！", null);
                }
                return ResultUtils.getResults(Result.Status.ERROR, "请做审核操作", null);
            }
            workScoreService.auditProdure( param.workScoreEntity);
            return ResultUtils.getSuccessResults();
          /*  if ("admin".equals(user)){
                str = workScoreService.adminPass(param.list);
                if (!"审核成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }
                return ResultUtils.getSuccessResults();
            }else {
                str = workScoreService.responsibleAllPass(param.list);
                if (!"审核成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }
                return ResultUtils.getSuccessResults();
            }*/
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }
}

class WorkScoreParam{
    public TokenEntity tokenEntity;
    public WorkScoreEntity workScoreEntity;
    public WorkScoreFilter workScoreFilter;
    public WorkSupervisionEntity workSupervisionEntity;
    public WorkSupervisionFilter workSupervisionFilter;
    public List<WorkScoreEntity> list;
}
